const fs = require('fs')
const path = require('path')
const jwt = require("jsonwebtoken");
const SECRET = "QWERTYUIOPasdfghjklZXCVBNM"
const secretKey = '&*^R*G&(FRDwp4eg3' //secret
const expiresIn = 60 * 60 * 24 * 30; //时效 (秒)

class Jwt {
    //生成jwt
    generateToken(data) {
            const token = jwt.sign({ data }, secretKey, {
                expiresIn
            })
            return token
        }
        //解析jwt
    verifyToken(token) {
            return jwt.verify(token, secretKey)
        }
        //计算剩余时间
    tokenExp(token) {
        let verify = this.verifyToken(token);
        let time = parseInt((new Date().getTime()) / 1000);
        return `剩余${verify.exp - time}秒`
    }

}


// let token = generateToken(1);

// console.log(token); //输出token
// console.log(verifyToken(token)); //输出token内容
// tokenExp(token);

module.exports = Jwt